//
//  FileViewController.m
//  笔记1
//
//  Created by  on 2022/11/6.
//

#import "FileViewController.h"
#import "HomeCell.h"
#import "EditViewController.h"
#import "MoreViewController.h"

@interface FileViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) NSMutableArray <Note *>*array;

@end

@implementation FileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self loaddata];
}
- (void)initUI{
    self.title = LS(self.model.name);
    [self.collectionView registerNib:[HomeCell nib] forCellWithReuseIdentifier:[HomeCell ID]];
    self.collectionView.c_edges.equalTo(self.view);
}
- (void)loaddata{
    [NoteTool findFileNote:self.model isDelete:self.model.isDelete complete:^(NSMutableArray<Note *> * _Nonnull result) {
        self.array = result;
        [self.collectionView reloadData];
    }];
}
- (void)addNotify{
    [NotifyTool add:self :@selector(loaddata) :notifyNoteUpdate];
}
#pragma mark - UIImagePickerControllerDelegate / UINavigationControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    UIImage *img = info[UIImagePickerControllerEditedImage];
    NSData *data = UIImageJPEGRepresentation(img, 0.5);
    WS(self);
    [SWTool swIMG:img type:ShiWuTypeOther success:^(NSDictionary * _Nonnull dict) {
        EditViewController *vc = [EditViewController new];
        Note *model = [Note new];
        [model.imgs addObject:data];
        model.stamp = [[NSDate currentTimeStamp:@"yyyy/MM/dd HH:mm:ss"] integerValue];
        model.isFile = NO;
        model.fileName = weakself.model.name;
        model.account = APP.user.account;
        if ([dict isKindOfClass:[NSDictionary class]] &&
            [[dict valueForKey:@"result"] isKindOfClass:[NSArray class]]) {
            NSArray *array = dict[@"result"];
            NSDictionary *dc = array[0];
            model.name = dc[@"keyword"];
            model.content = dc[@"root"];
        }else{
            model.name = dict[@"keyword"];
            model.content = dict[@"root"];
        }
        vc.model = model;
        vc.isedit = YES;
        [weakself push:vc :YES];
        NSLog(@"=====%@",dict);
    }];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.isadd ? self.array.count + 1 : self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HomeCell ID] forIndexPath:indexPath];
    if (indexPath.row == 0 && self.isadd) {
        cell.model=nil;
    }else{
        cell.model =  self.array[self.isadd ? indexPath.row - 1 : indexPath.row];
        cell.selectBtn.hidden = YES;
        cell.selectBtnW.constant = 0;
    }
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WS(self);
    if (indexPath.row == 0 && self.isadd) {
        MoreViewController *vc = [MoreViewController new];
        vc.icons = @[@"笔记",@"扫描"];
        vc.array = @[@"笔记",@"扫描"];
        vc.block = ^(NSString * _Nonnull title) {
            if ([title isEqualToString:@"笔记"]) {
                EditViewController *evc = [EditViewController new];
                Note *model = [Note new];
                model.account = APP.user.account;
                model.fileName = weakself.model.name;
                model.isFile = NO;
                evc.model = model;
                evc.isedit = YES;
                [weakself push:evc :YES];
            }else if ([title isEqualToString:@"扫描"]){
                dispatch_async(dispatch_get_main_queue(), ^{
                    [CCHud startHud];
                });
                UIImagePickerController *vc = [UIImagePickerController new];
                vc.delegate = weakself;
                vc.allowsEditing = YES;
                vc.sourceType = UIImagePickerControllerSourceTypeCamera;
                vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
                [weakself presentViewController:vc animated:YES completion:^{
                    [CCHud stopHud];
                }];
            }
        };
        [weakself presentViewController:vc animated:NO completion:nil];
    }else{
        EditViewController *vc = [EditViewController new];
        vc.model = self.array[self.isadd ? indexPath.row - 1 : indexPath.row];
        vc.isedit = NO;
        [self push:vc :YES];
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (SCREEN_WIDTH - 80) / 3;
    return  CGSizeMake(w, 140.0/92*w+42);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(20, 20, 20, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

@end
